package com.woman.beautylive.presentation.ui.main.index;

import com.woman.beautylive.data.bean.BaseResponse;
import com.woman.beautylive.data.bean.HotAnchorPageBean;
import com.woman.beautylive.domain.AnchorManager;
import com.woman.beautylive.presentation.ui.base.BaseActivity;
import com.woman.beautylive.presentation.ui.base.BaseObserver;
import com.woman.beautylive.presentation.ui.base.BasePresenter;
import com.woman.beautylive.presentation.ui.base.page.PageRecorder;
import com.woman.beautylive.presentation.ui.base.page.PagedPresenter;
import com.woman.beautylive.util.L;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author Muyangmin
 * @since 1.0.0
 * @see PagedPresenter
 */
public class HotAnchorPresenter extends BasePresenter<HotAnchorInterface> {

    private AnchorManager anchorManager;
    private PageRecorder pageRecorder;

    public HotAnchorPresenter(HotAnchorInterface uiInterface) {
        super(uiInterface);
        anchorManager = new AnchorManager();
        pageRecorder = new PageRecorder();
    }

    public void onBannerClicked(BaseActivity activity){

    }

//    第一张
    public void loadFirstPage() {
        Subscription subscription = anchorManager.loadHotAnchors(pageRecorder.getFirstPage())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<HotAnchorPageBean>>(getUiInterface()) {
                    @Override
                    public void onSuccess(BaseResponse<HotAnchorPageBean> response) {
                        pageRecorder.moveToFirstPage();
                        getUiInterface().displayBanners(response.getData().getBanner());
                        L.i("Presenter", response.toString());
                        getUiInterface().showData(response.getData().getList());
                    }
                });
        addSubscription(subscription);
    }

    //    第一张
    public void loadFirstPage(String token,String city,String sex) {
        Subscription subscription = anchorManager.loadHotAnchors(token, city, sex)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<HotAnchorPageBean>>(getUiInterface()) {
                    @Override
                    public void onSuccess(BaseResponse<HotAnchorPageBean> response) {
                        pageRecorder.moveToFirstPage();
                        getUiInterface().displayBanners(response.getData().getBanner());
                        L.i("Presenter", response.toString());
                        getUiInterface().showData(response.getData().getList());
                    }
                });
        addSubscription(subscription);
    }

//    第二张
    public void loadNextPage() {
        Subscription subscription = anchorManager.loadHotAnchors(pageRecorder.getNextPage())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<HotAnchorPageBean>>(getUiInterface()) {
                    @Override
                    public void onSuccess(BaseResponse<HotAnchorPageBean> response) {
                        pageRecorder.moveToNextPage();
                        if(response.getData()!=null) {
                            getUiInterface().displayBanners(response.getData().getBanner());
                            getUiInterface().appendData(response.getData().getList());
                        }
                    }
                });
        addSubscription(subscription);
    }

}
